package edu.spec.student.dao.impl;

import edu.spec.student.dao.iface.IStudentLessonDao;
import edu.spec.student.model.lesson.StudentLesson;
import org.madgecko.basic.dao.BaseDao;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class StudentLessonDao extends BaseDao<StudentLesson> implements IStudentLessonDao  {

    @Override
    public List<StudentLesson> listStudentLessonByCon(Integer lessonId, Integer signIn) {
        String hql = "";
        if(signIn!=null){
            hql = " select sl from StudentLesson sl where sl.lesson.id=" + lessonId + " and sl.signIn=" + signIn;
        }else{
            hql = " select sl from StudentLesson sl where sl.lesson.id=" + lessonId;
        }
        return this.list(hql);
    }

    @Override
    public StudentLesson loadByStudentLesson(Integer studentId, Integer lessonId) {
        String hql = " from StudentLesson sl where sl.student.id=" + studentId + " and sl.lesson.id=" + lessonId;
        return (StudentLesson) this.queryObject(hql);
    }

    @Override
    public void signIn(Integer studentId, Integer lessonId) {
        String hql = " update StudentLesson sl set sl.signIn=1 where sl.student.id=" + studentId + " and sl.lesson.id=" + lessonId;
        this.updateByHql(hql);
    }

    @Override
    public void deleteByLesson(Integer lessonId) {
        String hql = " delete StudentLesson sl where sl.lesson.id=" + lessonId;
        this.updateByHql(hql);
    }
}
